import 'package:flutter/material.dart';
import 'package:fyi/common/common_map.dart';
import 'package:fyi/dao/login_dao.dart';
import 'package:fyi/pages/history_page.dart';
import 'package:fyi/utils/common_util.dart';
import 'package:fyi/utils/navigator_util.dart';
import 'package:fyi/widget/code_review.dart';
import 'package:random_string/random_string.dart';

class LoginPage extends StatefulWidget {

  static final String sName = "/login";

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  String _codeStr = randomAlphaNumeric(4);

  @override
  void initState() {
    super.initState();
    _usernameController.text = "18621182327";
  }

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    _codeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        actions: <Widget>[
          Container(
            margin: EdgeInsets.only(right: 16),
            child: new GestureDetector(
                child: new Icon(Icons.language),
                onTap: () {
                  CommonUtil.showLanguageDialog(context, (locale){
                    CommonUtil.changeLanguage(locale);
                  });
                }),
          )
        ],
      ),
      body:Container(
        //decoration: BoxDecoration(color: Color.fromARGB(255, 28, 30, 38)),

        decoration: BoxDecoration(image: DecorationImage(image: AssetImage("assets/images/login_bg.png"),fit: BoxFit.cover)),
        alignment: Alignment.bottomCenter,
        child: _bottomView(),
      ),
    );
  }

  _bottomView(){
    return Container(
        //decoration: BoxDecoration(color: Color.fromARGB(255, 28, 30, 38)),
        alignment: Alignment.bottomCenter,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Expanded(child: _logoView(),),
            Container(
              margin: EdgeInsets.only(left: 62,right: 62),
              color: Colors.white,
              child: new TextField(
                controller: _usernameController,
                decoration: new InputDecoration(
                    border: new OutlineInputBorder(  //添加边框
                        gapPadding: 10.0,
                        borderRadius: BorderRadius.circular(0.0),
                        borderSide: BorderSide(width: 0,color: Colors.white,style: BorderStyle.none)
                    ),
                    hintText: CommonUtil.getS(context).please_enter_username,
                    contentPadding: const EdgeInsets.all(10.0)),
                keyboardType: TextInputType.text,
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 62,right: 62,top: 6),
              color: Colors.white,
              child: new TextField(
                controller: _passwordController,
                decoration: new InputDecoration(
                    border: new OutlineInputBorder(  //添加边框
                        gapPadding: 10.0,
                        borderRadius: BorderRadius.circular(0.0),
                        borderSide: BorderSide(width: 0,color: Colors.white,style: BorderStyle.none)
                    ),
                    hintText: CommonUtil.getS(context).please_enter_password,
                    contentPadding: const EdgeInsets.all(10.0)),
                keyboardType: TextInputType.text,
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 62,right: 62,top: 6),
              child: Row(
                children: <Widget>[
                  Expanded(child: Container(
                    color: Colors.white,
                    child: new TextField(
                      controller: _codeController,
                      decoration: new InputDecoration(
                          border: new OutlineInputBorder(  //添加边框
                            gapPadding: 10.0,
                            borderRadius: BorderRadius.circular(2.0),
                          ),
                          hintText: CommonUtil.getS(context).please_enter_verification_code,
                          contentPadding: const EdgeInsets.all(10.0)),
                      keyboardType: TextInputType.text,
                    ),
                  ),),
                  Container(
                    padding: EdgeInsets.only(left: 6),
                    //child: Image.network("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg",width: 100,height: 40,fit: BoxFit.cover,),
                    child: CodeReview(
                      text:   _codeStr,
                      onTap: (text) {
                        setState(() {
                          _codeStr = text;
                        });
                      },
                    ),
                  )
                ],
              ),
            ),
            _wrapTap(Container(
              width: 120,
              height: 48,
              margin: EdgeInsets.only(top: 40),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(24),
                  color:Colors.blue
              ),
              child: Center(
                child: Text(CommonUtil.getS(context).login,style: TextStyle(color: Colors.white,fontSize: 16),),
              ),
            ), (){
              _login(context);
            }),
            Container(
              margin: EdgeInsets.only(top: 36,bottom: 28),
              child: Center(
                child: Text(CommonUtil.getS(context).copyright,style: TextStyle(color: Colors.white,fontSize: 14),),
              ),
            )
          ],
        )
    );
  }

  _logoView(){
    return Container(
      height: 180,
      margin: EdgeInsets.only(top: 0,bottom: 60),
      alignment: Alignment.center,
      child: Image.asset("assets/images/ic_tag_2.png",width:180,height: 68),
    );
  }

  _wrapTap(Container container, void Function() click) {
    return GestureDetector(
      child: container,
      onTap: (){
        if(click != null){
          click();
        }
      },
    );
  }

  void _login(BuildContext context) {
    String username = _usernameController.text;
    String password = _passwordController.text;
    String code = _codeController.text;
    if(username == ""){
      CommonUtil.toast(CommonUtil.getS(context).please_enter_username);
      return;
    }
    if(password == ""){
      CommonUtil.toast(CommonUtil.getS(context).please_enter_password);
      return;
    }
    if(code == ""){
      CommonUtil.toast(CommonUtil.getS(context).please_enter_verification_code);
      return;
    }
    if(code != _codeStr){
      CommonUtil.toast(CommonUtil.getS(context).ver_code_error);
      return;
    }
    LoginDao.fetch(username, password).then((value){
      if(value?.hasToken()){
        CommonUtil.saveToken(value.accessToken);
        NavigatorUtil.push(context, HistoryPage());
      }
    }).catchError((err){
      CommonUtil.toast(err.message);
    });
  }
}
